<?php

/**
 * 自动生成 WxEventAwardSnService
 * 
 * Apps/Common/Service/WxEventAwardSnService.class.php
 * cmcc_admin @ 2015-07-20
 */

namespace Common\Service;

class WxEventAwardSnService {

  private static $instance;

  var $status = array(
    0 => '未兑换',
    1 => '已兑换',
  );
  
  public static function instance() {
    if (self::$instance == null) {
      $instance = new WxEventAwardSnService();
    }

    return $instance;
  }

  function get_default_row() {
    return array(
      'award_id' => '',
      'sn' => '',
      'status' => '0',
      'create_time' => date('Y-m-d H:i:s'),
      'take_time' => '',
    );
  }

  function get_by_id($id) {
    $WxEventAwardSn = M('WxEventAwardSn');
    $data = $WxEventAwardSn->find($id);
    return $data ? $data : array();
  }

  function get_by_cond($config) {
    $default = array(
      'page' => 1,
      'page_size' => 6,
      'status' => '',
      'award_id' => 0 ,
      'count' => FALSE,
      'order' => 'DESC',
      'sort' => 'id',
    );

    $config = extend($config, $default);

    $WxEventAwardSn = M('WxEventAwardSn');

    $where = array();

    if (!empty($config['status'])) {
      $where['status'] = $config['status'];
    }
    
    if (!empty($config['award_id'])) {
      $where['award_id'] = $config['award_id'];
    }

    if ($config['count']) {
      return $WxEventAwardSn->where($where)->count();
    } else {
      $order = $config['sort'] . ' ' . $config['order'];
      $limit = ($config['page'] - 1 ) * $config['page_size'] . ' , ' . $config['page_size'];
      $data = $WxEventAwardSn
          ->where($where)
          ->limit($limit)->order($order)->select();
      //echo $WxEventAwardSn->_sql();
      return $data ? $data : array();
    }
  }

  function create($data, $is_ajax = true) {
    $WxEventAwardSn = M('WxEventAwardSn');
    $ret = $WxEventAwardSn->add($data);
    if ($is_ajax) {
      if ($ret) {
        return ajax_arr('添加成功', TRUE, array(
          'id' => $ret
        ));
      } else {
        return ajax_arr('添加失败', FALSE);
      }
    } else {
      if ($ret) {
        return $ret;
      } else {
        return 0;
      }
    }
  }
  
  function create_by_excel( $award_id , $filename ) {
    $Excel = ExcelService::instance();
    $excel_data = $Excel->import_to_array($filename);

    $data = array();
    foreach ( $excel_data as $val) {
      $data[] = array(
        'award_id' => $award_id ,
        'sn' => $val['A'],
      );
    }
    
    $WxEventAwardSn = M('WxEventAwardSn');
    $ret = $WxEventAwardSn->addAll($data);
    if ( $ret ) {
      return ajax_arr('Excel导入成功', TRUE );
    } else {
      return ajax_arr('Excel导入失败');
    }
  }

  function update($id, $data) {
    $WxEventAwardSn = M('WxEventAwardSn');

    $ret = $WxEventAwardSn->where("id = %d", $id)->save($data);
    if ($ret) {
      return ajax_arr('编辑成功', TRUE);
    } else {
      return ajax_arr('编辑失败', FALSE);
    }
  }

  function delete($ids) {
    $WxEventAwardSn = M('WxEventAwardSn');
    $ret = $WxEventAwardSn->delete($ids);
    if ($ret == 0) {
      return ajax_arr('未删除任何数据', FALSE);
    } else if (!$ret) {
      return ajax_arr('删除失败', FALSE);
    } else {
      return ajax_arr('删除' . $ret . '行数据', TRUE);
    }
  }

  function get_one_sn( $award_id ) {
    $WxEventAwardSn = M('WxEventAwardSn');
    $data = $WxEventAwardSn->where('award_id = %d and status = 0 ' , $award_id )->order('id ASC')->find();
    if ( empty( $data ) ) {
      return '';
    }
    $ret = $WxEventAwardSn->where( 'id = %d ' ,  $data['id'] )->save(array(
      'status' => 1 ,
      'take_time' => date('Y-m-d H:i:s')
    ));
    if ( $ret ) {
      return $data['sn'] ;
    } else {
      return '' ;
    }
  }
}
